package com.turing.post.antimoney.impl;

import java.math.BigInteger;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.antimoney.util.SuspiciousTrans;
import com.turing.post.antimoney.util.SuspiciousTransDao;
import com.turing.post.util.Config;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

/**
 * @author 李治
 * 
 */
@Remote
@Local
@Stateless(mappedName = "antimoney.impl.SuspiciousTransDaoImpl")
public class SuspiciousTransDaoImpl implements SuspiciousTransDao {

	/**
	 * 实体管理器
	 * 
	 * @see javax.persistence.EntityManager
	 */
	@PersistenceContext
	protected EntityManager _em;

	@Override
	public void insertSuspiciousTrans(SuspiciousTrans suspiciousTrans)
			throws PCPSApplicationException {
		GeneralDao.insert(_em, suspiciousTrans);
	}

	@Override
	public void updateSuspiciousTrans(SuspiciousTrans suspiciousTrans)
			throws PCPSApplicationException {
		GeneralDao.update(_em, suspiciousTrans);
	}

	@Override
	public void deleteSuspiciousTrans(SuspiciousTrans suspiciousTrans)
			throws PCPSApplicationException {
		GeneralDao.remove(_em, suspiciousTrans);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SuspiciousTrans> getSuspiciousTransListByAlarmInfoId(
			BigInteger alarmInfoId) {
		String sql = "select * from SuspiciousTrans t where t.alarminfoid="
				+ alarmInfoId + " order by t.seqno";
		Query query = _em.createNativeQuery(sql, SuspiciousTrans.class);
		return query.getResultList();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Object[]> getSuspiciousTransByCondition(PCPSWebInput webInput,
			String alarmRuleType, String startAlarmTime, String endAlarmTime,
			String reportStatus, String startReportTime, String endReportTime,
			String suspiciousName, String suspiciousId, BigInteger startAmount,
			BigInteger endAmount, Integer page) {
		String sqlAlarmTime = "";
		if (!PCPSUtil.isEmpty(startAlarmTime))
			sqlAlarmTime += " and (a.alarmtime is null or a.alarmtime>=to_date('"
					+ startAlarmTime + " 00:00:00','yyyy-MM-dd hh24:mi:ss')) ";
		if (!PCPSUtil.isEmpty(endAlarmTime))
			sqlAlarmTime += " and (a.alarmtime is null or a.alarmtime<=to_date('"
					+ endAlarmTime + " 23:59:59','yyyy-MM-dd hh24:mi:ss')) ";
		String sqlReportTime = "";
		if (!PCPSUtil.isEmpty(startReportTime))
			sqlReportTime += " and (sr.reportdate is null or sr.reportdate>=to_date('"
					+ startReportTime + " 00:00:00','yyyy-MM-dd hh24:mi:ss')) ";
		if (!PCPSUtil.isEmpty(endReportTime))
			sqlReportTime += " and (sr.reportdate is null or sr.reportdate<=to_date('"
					+ endReportTime + " 23:59:59','yyyy-MM-dd hh24:mi:ss')) ";
		String sql = "select t.reportid reportid,t.alarminfoid alarminfoid,t.seqno seqno,ar.alarmruletype alarmRuleType,"
				+ "t.tstm tstm,t.ctnm ctnm,t.ctid ctid,t.crat crat,"
				+ "(case when t.reportid is null then '0' when t.reportid is not null then sr.reportstatus else '1' end) reportstatus "
				+ "from SUSPICIOUSTRANS t left join alarminfo a on a.alarminfoid = t.alarminfoid "
				+ "left join alarmrule ar on ar.alarmruleid = a.alarmruleid "
				+ "left join suspiciousreport sr on sr.reportid = t.reportid where 1=1 "
				+ sqlAlarmTime + sqlReportTime;
		if (!PCPSUtil.isEmpty(alarmRuleType)) {
			sql += " and ar.alarmruletype='" + alarmRuleType + "'";
		}
		if (!PCPSUtil.isEmpty(reportStatus)) {
			sql += " and sr.reportstatus='" + reportStatus + "'";
		}
		if (!PCPSUtil.isEmpty(suspiciousName)) {// 可疑主体名称、交易对手银行账号的开户银行名称、交易商品名称、交易对手所在支付机构银行账号的开户银行名称、可疑主体银行账号的开户银行名称、可疑主体的法定代表人姓名（对公客户）、交易对手姓名/名称
			sql += " and (t.ctnm like '%" + suspiciousName
					+ "%' or t.crnm like '%" + suspiciousName
					+ "%' or t.cabm like '%" + suspiciousName
					+ "%' or t.tcnm like '%" + suspiciousName
					+ "%' or t.tcbn like '%" + suspiciousName
					+ "%' or t.tpbn like '%" + suspiciousName
					+ "%' or t.tmnm like '%" + suspiciousName + "%')";
		}
		if (!PCPSUtil.isEmpty(suspiciousId)) {// 可疑主体证件号、交易对手证件/证明文件号码、可疑主体身份证件/证明文件号码、可疑主体的法定代表人身份证件号码（对公客户）
			sql += " and (t.ctid='" + suspiciousId + "' or t.crid ='"
					+ suspiciousId + "' or t.tcid='" + suspiciousId
					+ "' or t.ctid='" + suspiciousId + "')";
		}
		if (null != startAmount) {
			sql += " and t.crat >=" + startAmount;
		}
		if (null != endAmount) {
			sql += " and t.crat <=" + endAmount;
		}
		sql += " order by t.alarminfoid,t.seqno";
		Query query = _em.createNativeQuery(sql);
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		query.setFirstResult(startPosition);
		query.setMaxResults(maxResult);
		return query.getResultList();
	}

	@Override
	public Integer getSuspiciousTransCountByCondition(PCPSWebInput webInput,
			String alarmRuleType, String startAlarmTime, String endAlarmTime,
			String reportStatus, String startReportTime, String endReportTime,
			String suspiciousName, String suspiciousId, BigInteger startAmount,
			BigInteger endAmount) {
		String sqlAlarmTime = "";
		if (!PCPSUtil.isEmpty(startAlarmTime))
			sqlAlarmTime += " and (a.alarmtime is null or a.alarmtime>=to_date('"
					+ startAlarmTime + " 00:00:00','yyyy-MM-dd hh24:mi:ss')) ";
		if (!PCPSUtil.isEmpty(endAlarmTime))
			sqlAlarmTime += " and (a.alarmtime is null or a.alarmtime<=to_date('"
					+ endAlarmTime + " 23:59:59','yyyy-MM-dd hh24:mi:ss')) ";
		String sqlReportTime = "";
		if (!PCPSUtil.isEmpty(startReportTime))
			sqlReportTime += " and (sr.reportdate is null or sr.reportdate>=to_date('"
					+ startReportTime + " 00:00:00','yyyy-MM-dd hh24:mi:ss')) ";
		if (!PCPSUtil.isEmpty(endReportTime))
			sqlReportTime += " and (sr.reportdate is null or sr.reportdate<=to_date('"
					+ endReportTime + " 23:59:59','yyyy-MM-dd hh24:mi:ss')) ";
		String sql = "select nvl(count(*),0) from SUSPICIOUSTRANS t "
				+ "left join alarminfo a on a.alarminfoid = t.alarminfoid left join alarmrule ar on ar.alarmruleid = a.alarmruleid "
				+ "left join suspiciousreport sr on sr.reportid = t.reportid where 1=1 "
				+ sqlAlarmTime + sqlReportTime;
		if (!PCPSUtil.isEmpty(alarmRuleType)) {
			sql += " and ar.alarmruletype='" + alarmRuleType + "'";
		}
		if (!PCPSUtil.isEmpty(reportStatus)) {
			sql += " and sr.reportstatus='" + reportStatus + "'";
		}
		if (!PCPSUtil.isEmpty(suspiciousName)) {// 可疑主体名称、交易对手银行账号的开户银行名称、交易商品名称、交易对手所在支付机构银行账号的开户银行名称、可疑主体银行账号的开户银行名称、可疑主体的法定代表人姓名（对公客户）、交易对手姓名/名称
			sql += " and (t.ctnm like '%" + suspiciousName
					+ "%' or t.crnm like '%" + suspiciousName
					+ "%' or t.cabm like '%" + suspiciousName
					+ "%' or t.tcnm like '%" + suspiciousName
					+ "%' or t.tcbn like '%" + suspiciousName
					+ "%' or t.tpbn like '%" + suspiciousName
					+ "%' or t.tmnm like '%" + suspiciousName + "%')";
		}
		if (!PCPSUtil.isEmpty(suspiciousId)) {// 可疑主体证件号、交易对手证件/证明文件号码、可疑主体身份证件/证明文件号码、可疑主体的法定代表人身份证件号码（对公客户）
			sql += " and (t.ctid='" + suspiciousId + "' or t.crid ='"
					+ suspiciousId + "' or t.tcid='" + suspiciousId
					+ "' t.ctid='" + suspiciousId + "')";
		}
		if (null != startAmount) {
			sql += " and t.crat >=" + startAmount;
		}
		if (null != endAmount) {
			sql += " and t.crat <=" + endAmount;
		}
		Query query = _em.createNativeQuery(sql);
		return Integer.parseInt(query.getSingleResult().toString());
	}

	@Override
	public SuspiciousTrans getSuspiciousTransById(
			SuspiciousTrans suspiciousTrans) {
		return _em.find(SuspiciousTrans.class, suspiciousTrans);
	}
	/**
	 * 根据reportidID查询
	 * @param reportidId
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<SuspiciousTrans> getSuspiciousTransListByReportId(
			BigInteger reportid){
		if(reportid==null){
			return null;
		}
		String sql="select * from SuspiciousTrans where reportid=?1";
		Query query=_em.createNativeQuery(sql, SuspiciousTrans.class);
		query.setParameter(1, reportid);
		return query.getResultList();		
	}
}
